//
//  WelcomeViewController.swift
//  Weibo
//
//  Created by gdarkness on 16/3/26.
//  Copyright © 2016年 gdarkness. All rights reserved.
//

import UIKit

import SnapKit

class WelcomeViewController: UIViewController {
    //MARK: -- 属性
    var iconViewBottomCons : NSLayoutConstraint?

    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
    }
    private func prepareUI() {
        //添加子控件
        view.addSubview(backgrounImageView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        //添加约束
        backgrounImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
        
        //背景
        backgrounImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.view)
        }
//        self.view.addConstraint(NSLayoutConstraint(item: backgrounImageView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
//        
//        self.view.addConstraint(NSLayoutConstraint(item: backgrounImageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
//        
//        self.view.addConstraint(NSLayoutConstraint(item: backgrounImageView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
//        
//        self.view.addConstraint(NSLayoutConstraint(item: backgrounImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        //头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-160)
        }
//        self.view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        
//        //底部约束
//       iconViewBottomCons =  (NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
//        self.view.addConstraint(iconViewBottomCons!)
        
        //欢迎Label
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
//         self.view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        
//         self.view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
    }
    
    //界面已经显示了
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        moveAnimation()
        
    }
    
    private func moveAnimation() {
        //动画
        //usingSpringWithDamping :弹簧明显程度 0-1 数值越小，弹簧效果越明显
        //initialSpringVelocity : 初速度
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            //self.iconViewBottomCons?.constant = -(UIScreen.mainScreen().bounds.height - 160)
            self.iconView.snp_makeConstraints(closure: { (make) -> Void in
                make.bottom.equalTo(self.view).offset(-(UIScreen.mainScreen().bounds.height - 160))
            })
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                UIView.animateWithDuration(DefaulDuration, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        print("切换动画完成")
                        AppDelegate.outSwitchRootViewController(MainViewController())
                })
        }
    }
    
    //MARK: -- lazyload
    //背景
    private lazy var backgrounImageView : UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconView:UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        //设置圆角
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    
    private lazy var welcomeLabel : UILabel = {
        let label = UILabel()
        label.text = "艹你妹"
        label.font = UIFont.systemFontOfSize(14)
        label.sizeToFit()
        label.alpha = 0
        
        return label
    }()

}
